package com.fk.his.domain.model.aggregates;

import com.fk.his.domain.common.IDomainCRUD;
import com.fk.his.domain.common.exception.BusinessException;
import com.fk.his.domain.common.factory.RepositoryFactory;
import com.fk.his.domain.model.vo.BizNoRuleVo;
import com.fk.his.domain.repository.IBizNoRuleRepository;
import com.rickie.dto.data.BizNoRuleAddCmd;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.sql.Date;
import java.util.Objects;

/**
 * <p>
 * 序号生成规则
 * <p>
 * 示例代码
 * <pre>
 * </pre/>
 *
 * @author InHome
 * @version BME V100R001 2023/03/27
 * @since BME V100R001C40B104
 */
@Getter
@AllArgsConstructor
@NoArgsConstructor
public class BizNoRule implements IDomainCRUD<BizNoRule> {


    /**
     * id
     */
    private Long id;

    /**
     * 组织机构代码
     */
    private Long orgId;
    /**
     * 业务名
     */
    private String ruleName;

    /**
     * 业务代码
     */
    private String ruleCode;
    /**
     * 前缀
     */
    private String prefix;
    /**
     * 后缀
     */
    private String suffix;
    /**
     * 日期占位格式,yyyyMM,yyyyMMdd等
     */
    private String datePattern;
    /**
     * 序号长度,例：长度5，当前值：3则序号结果为00003
     */
    private Integer serialLength;
    /**
     * 步长
     */
    private Integer step;
    /**
     * 当前值
     */
    private Long currentNo;
    /**
     * 初始化周期，1:日,2:周,3:月,4:年,5:账期
     */
    private String initCycle;
    /**
     * 最后获取日期
     */
    private Date lastTime;
    /**
     * 子对象
     * todo 新增加的
     */
    private BizNoRuleVo bizNoRuleVo;

    public BizNoRule(BizNoRuleAddCmd bizNoRuleDTO) {
        this.id = bizNoRuleDTO.getId();
        this.orgId = bizNoRuleDTO.getOrgId();
        this.ruleName = bizNoRuleDTO.getRuleName();
        this.ruleCode = bizNoRuleDTO.getRuleCode();
        this.prefix = bizNoRuleDTO.getPrefix();
        this.suffix = bizNoRuleDTO.getSuffix();
        this.datePattern = bizNoRuleDTO.getDatePattern();
        this.serialLength = bizNoRuleDTO.getSerialLength();
        this.step = bizNoRuleDTO.getStep();
        this.currentNo = bizNoRuleDTO.getCurrentNo();
        this.initCycle = bizNoRuleDTO.getInitCycle();
    }

    @Override
    public boolean delete() {
        IBizNoRuleRepository iBizNoRuleRepository = RepositoryFactory.get(IBizNoRuleRepository.class);
//        iBizNoRuleRepository
        return false;
    }

    @Override
    public BizNoRule fetch() {
        if (Objects.isNull(id)) {
            throw new BusinessException("获取实体时，用户id和用户名不能同时为空");
        }
        IBizNoRuleRepository iBizNoRuleRepository = RepositoryFactory.get(IBizNoRuleRepository.class);
        return iBizNoRuleRepository.findById(id);
    }

    @Override
    public BizNoRule saveOrUpdate() {
        IBizNoRuleRepository iBizNoRuleRepository = RepositoryFactory.get(IBizNoRuleRepository.class);
        iBizNoRuleRepository.save(this);
        return this;
    }
}